home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / nroff1.zip / NROTXT.C < prev    next >
C/C++ Source or Header  |  1990-12-07  |  14KB  |  635 lines

  1. /*
  2.  *      Text processing portion of NRO word processor
  3.  *
  4.  *      Stephen L. Browning
  5.  *      5723 North Parker Avenue
  6.  *      Indianapolis, Indiana 46220
  7.  *
  8.  *    Ported to MS C 5.1 
  9.  *      by John Dudeck (jdudeck@polyslo.calpoly.edu) 11/25/90.
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include "nro.h"
  14. #include "nroxtrn.h"
  15.  
  16. text(p)
  17. char *p;
  18. {
  19.         int i;
  20.         char wrdbuf[MAXLINE];
  21.  
  22.         if (*p == ' ' || *p == '\n' || *p == '\r') leadbl(p);
  23.         expesc(p,wrdbuf);
  24.         if (dc.ulval > 0) {
  25.                 /*
  26.                 *       Because of the way underlining is handled,
  27.                 *       MAXLINE should be declared to be three times
  28.                 *       larger than the longest expected input line
  29.                 *       for underlining.  Since many of the character
  30.                 *       buffers use this parameter, a lot of memory
  31.                 *       can be allocated when it may not really be
  32.                 *       needed.  A MAXLINE of 180 would allow about
  33.                 *       60 characters in the output line to be
  34.                 *       underlined (remember that only alphanumerics
  35.                 *       get underlined - no spaces or punctuation).
  36.                 */
  37.                 if (dc.buflg) underl(p,wrdbuf,MAXLINE);
  38.                 --dc.ulval;
  39.         }
  40.         if (dc.cuval > 0) {
  41.                 if (dc.buflg) underl(p,wrdbuf,MAXLINE);
  42.                 --dc.cuval;
  43.         }
  44.         if (dc.boval > 0) {
  45.                 if (dc.buflg) bold(p,wrdbuf,MAXLINE);
  46.                 --dc.boval;
  47.         }
  48.         if (dc.ceval > 0) {
  49.                 center(p);
  50.                 put(p);
  51.                 --dc.ceval;
  52.         }
  53.         else if (*p == '\r' || *p == '\n') put(p); /* all blank line */
  54.         else if (dc.fill == NO) put(p);         /* unfilled */
  55.         else {
  56.                 while ((i = getwrd(p,wrdbuf)) > 0) {
  57.                         putwrd(wrdbuf);
  58.                         p += i;
  59.                 }
  60.         }
  61. }
  62.  
  63.  
  64. /*
  65.  *      insert bold face text
  66.  */
  67.  
  68. bold(p0,p1,size)
  69. char *p0, *p1;
  70. int size;
  71. {
  72.         int i, j;
  73.  
  74.         j = 0;
  75.         for (i=0; (p0[i] != '\n') && (j < size-1); ++i) {
  76.                 if (isalpha(p0[i]) || isdigit(p0[i])) {
  77.                         p1[j++] = p0[i];
  78.                         p1[j++] = '\b';
  79.                 }
  80.                 p1[j++] = p0[i];
  81.         }
  82.         p1[j++] = '\n';
  83.         p1[j] = EOS;
  84.         while (*p1 != EOS) *p0++ = *p1++;
  85.         *p0 = EOS;
  86. }
  87.  
  88.  
  89.  
  90.  
  91. /*
  92.  *      center a line by setting tival
  93.  */
  94.  
  95. center(p)
  96. char *p;
  97. {
  98.         dc.tival = max((dc.rmval + dc.tival - width(p)) >> 1,0);
  99. }
  100.  
  101.  
  102. /*
  103.  *      expand title buffer to include character string
  104.  */
  105.  
  106. expand(p0,c,s)
  107. char *p0;
  108. char c;
  109. char *s;
  110. {
  111.         char tmp[MAXLINE];
  112.         char *p, *q, *r;
  113.  
  114.         p = p0;
  115.         q = tmp;
  116.         while (*p != EOS) {
  117.                 if (*p == c) {
  118.                         r = s;
  119.                         while (*r != EOS) *q++ = *r++;
  120.                 }
  121.                 else *q++ = *p;
  122.                 ++p;
  123.         }
  124.         *q = EOS;
  125.         strcpy(p0,tmp);         /* copy it back */
  126. }
  127.  
  128.  
  129. /*
  130.  *      get field from title
  131.  */
  132.  
  133. char *getfield(p,q,delim)
  134. char *p, *q;
  135. char delim;
  136. {
  137.         while (*p != delim && *p != '\r' && *p != '\n' && *p != EOS) {
  138.                 *q++ = *p++;
  139.         }
  140.         *q = EOS;
  141.         if (*p == delim) ++p;
  142.         return(p);
  143. }
  144.  
  145.  
  146.  
  147. /*
  148.  *      get non-blank word from p0 into p1.
  149.  *      return number of characters processed.
  150.  */
  151.  
  152. getwrd(p0,p1)
  153. char *p0,*p1;
  154. {
  155.         int i;
  156.         char *p, c;
  157.  
  158.         i = 0;
  159.         while (*p0 == ' ' || *p0 == '\t') {
  160.                 ++i;
  161.                 ++p0;
  162.         }
  163.         p = p0;
  164.         while (*p0 != ' ' && *p0 != EOS && *p0 != '\t') {
  165.                 if (*p0 == '\n' || *p0 == '\r') break;
  166.                 *p1 = *p0++;
  167.                 ++p1;
  168.                 ++i;
  169.         }
  170.         c = *(p1-1);
  171.         if (c == '"') c = *(p1-2);
  172.         if (c == '?' || c == '!') {
  173.                 *p1++ = ' ';
  174.                 ++i;
  175.         }
  176.         if (c == '.' && (*p0 == '\n' || *p0 == '\r' || islower(*p))) {
  177.                 *p1++ = ' ';
  178.                 ++i;
  179.         }
  180.         *p1 = EOS;
  181.         return(i);
  182. }
  183.  
  184.  
  185. /*
  186.  *      convert integer to decimal ascii string
  187.  */
  188.  
  189. itoda(value,p,size)
  190. int value;
  191. char *p;
  192. int size;
  193. {
  194.         char c[7];
  195.         int i, j, k;
  196.         int aval;
  197.  
  198.         aval = abs(value);
  199.         c[0] = EOS;
  200.         i = 1;
  201.         do {
  202.                 c[i++] = (aval % 10) + '0';
  203.                 aval /= 10;
  204.         } while (aval > 0 && i <= size);
  205.         if (value < 0 && i <= size) c[i++] = '-';
  206.         for (j=0; j<i; ++j) *p++ = c[i-j-1];
  207.         return(i);
  208. }
  209.  
  210.  
  211. /*
  212.  *      center title text into print buffer
  213.  */
  214.  
  215. justcntr(p,q,limit)
  216. char *p, *q;
  217. int limit[];
  218. {
  219.         int len;
  220.  
  221.         len = width(p);
  222.         q = &q[(limit[RIGHT] + limit[LEFT] - len) >> 1];
  223.         while (*p != EOS) *q++ = *p++;
  224. }
  225.  
  226.  
  227.  
  228. /*
  229.  *      left justify title text into print buffer
  230.  */
  231.  
  232. justleft(p,q,limit)
  233. char *p, *q;
  234. int limit;
  235. {
  236.         q = &q[limit];
  237.         while (*p != EOS) *q++ = *p++;
  238. }
  239.  
  240.  
  241. /*
  242.  *      right justify title text into print buffer
  243.  */
  244.  
  245. justrite(p,q,limit)
  246. char *p, *q;
  247. int limit;
  248. {
  249.         int len;
  250.  
  251.         len = width(p);
  252.         q = &q[limit - len];
  253.         while (*p != EOS) *q++ = *p++;
  254. }
  255.  
  256.  
  257.  
  258.  
  259. /*
  260.  *      delete leading blanks, set tival
  261.  */
  262.  
  263. leadbl(p)
  264. char *p;
  265. {
  266.         int i,j;
  267.  
  268.         brk();
  269.         for (i=0; p[i] == ' '; ++i) ;
  270.         if (p[i] != '\n' && p[i] != '\r') dc.tival = i;
  271.         for (j=0; p[i] != EOS; ++j) p[j] = p[i++];
  272.         p[j] = EOS;
  273. }
  274.  
  275.  
  276.  
  277. /*
  278.  *      find minimum of two integer
  279.  */
  280.  
  281. min(v1,v2)
  282. int v1,v2;
  283. {
  284.         return((v1 < v2) ? v1 : v2);
  285. }
  286.  
  287.  
  288.  
  289. /*
  290.  *      find maximum of two integers
  291.  */
  292.  
  293. max(v1,v2)
  294. int v1,v2;
  295. {
  296.         return((v1 > v2) ? v1 : v2);
  297. }
  298.  
  299.  
  300.  
  301. /*
  302.  *      put out page footer
  303.  */
  304.  
  305. pfoot()
  306. {
  307.         if (dc.prflg == TRUE) {
  308.                 skip(pg.m3val);
  309.                 if (pg.m4val > 0) {
  310.                         if ((pg.curpag % 2) == 0) {
  311.                                 puttl(pg.efoot,pg.eflim,pg.curpag);
  312.                         }
  313.                         else {
  314.                                 puttl(pg.ofoot,pg.oflim,pg.curpag);
  315.                         }
  316.                         skip(pg.m4val - 1);
  317.                 }
  318.         }
  319. }
  320.  
  321.  
  322.  
  323. /*
  324.  *      put out page header
  325.  */
  326.  
  327. phead()
  328. {
  329.         pg.curpag = pg.newpag;
  330.         if (pg.curpag >= pg.frstpg && pg.curpag <= pg.lastpg) {
  331.                 dc.prflg = TRUE;
  332.         }
  333.         else {
  334.                 dc.prflg = FALSE;
  335.         }
  336.         ++pg.newpag;
  337.         if (dc.prflg == TRUE) {
  338.                 if (pg.m1val > 0) {
  339.                         skip(pg.m1val - 1);
  340.                         if ((pg.curpag % 2) == 0) {
  341.                                 puttl(pg.ehead,pg.ehlim,pg.curpag);
  342.                         }
  343.                         else {
  344.                                 puttl(pg.ohead,pg.ohlim,pg.curpag);
  345.                         }
  346.                 }
  347.                 skip(pg.m2val);
  348.         }
  349.         /*
  350.         *       initialize lineno for the next page
  351.         */
  352.         pg.lineno = pg.m1val + pg.m2val + 1;
  353. }
  354.  
  355.  
  356. /*
  357.  *      print character with test for printer
  358.  */
  359.  
  360. prchar(c,fp)
  361. char c;
  362. FILE *fp;
  363. {
  364.         putc(c,fp);
  365. }
  366.  
  367.  
  368.  
  369.  
  370. /*
  371.  *      put out line with proper spacing and indenting
  372.  */
  373.  
  374. put(p)
  375. char *p;
  376. {
  377.         char os[MAXLINE];
  378.         int j;
  379.  
  380.         if (pg.lineno == 0 || pg.lineno > pg.bottom) {
  381.                 phead();
  382.         }
  383.         if (dc.prflg == TRUE) {
  384.                 if (!dc.bsflg) {
  385.                         if (strkovr(p,os) == TRUE) {
  386.                                 for (j=0; j<pg.offset; ++j) prchar(' ',pout);
  387.                                 for (j=0; j<dc.tival; ++j) prchar(' ',pout);
  388.                                 putlin(os,pout);
  389.                         }
  390.                 }
  391.                 for (